from app import db
from datetime import datetime

class UserRole(db.Model):
    """用户角色关联表"""
    __tablename__ = 'user_roles'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 唯一约束
    __table_args__ = (db.UniqueConstraint('user_id', 'role_id', name='uq_user_role'),)
    
    def __repr__(self):
        return f'<UserRole {self.user_id}:{self.role_id}>' 